//
//  NSDate+FLAdditions.m
//  QNEngine
//
//  Created by victor on 13-3-28.
//  Copyright (c) 2013年 sunline. All rights reserved.
//

#import "NSDate+Additions.h"


@implementation NSDate (Additions)

- (NSString *)timeString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setFormatterBehavior: NSDateFormatterBehavior10_4];
    NSTimeZone *hkTimeZone = [NSTimeZone timeZoneWithName: @"Asia/Hong_Kong"];
    dateFormatter.timeZone = hkTimeZone;
    [dateFormatter setDateFormat:@"HH:mm"];
    return [dateFormatter stringFromDate: self];
}


+ (uint32_t)normalizedToDayBeginForTime:(uint32_t)time withCalendar:(NSCalendar *)calendar
{
    NSCalendarUnit calUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dc = [calendar components:calUnit fromDate:[NSDate dateWithTimeIntervalSince1970:time]];
    dc.hour = 0;
    dc.minute = 0;
    dc.second = 0;
    return [[calendar dateFromComponents:dc] timeIntervalSince1970];
}

+ (uint32_t)normalizedToDayEndForTime:(uint32_t)time withCalendar:(NSCalendar *)calendar
{
    NSCalendarUnit calUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dc = [calendar components:calUnit fromDate:[NSDate dateWithTimeIntervalSince1970:time]];
    dc.hour = 23;
    dc.minute = 59;
    dc.second = 59;
    return [[calendar dateFromComponents:dc] timeIntervalSince1970];
}


- (uint64_t)timeIntervalSince1970_MSEC
{
    uint64_t mps = 1000;
    return [self timeIntervalSince1970] * mps;
}


- (NSString *)timerShaftString
{
    NSTimeInterval ts = [self timeIntervalSince1970];
    return [[self class] timerShaftStringWithTimeStamp: ts];
}

- (NSString *)imTimerShaftString {
    NSTimeInterval ts = [self timeIntervalSince1970];
    return [[self class] imTimerShaftStringWithTimeStamp: ts];
}

+ (NSString *)timerShaftStringWithTimeStamp:(NSTimeInterval)ts
{

    @try {
        //实例化一个NSDateFormatter对象
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
        
        NSDate * nowDate = [self getLocalDateWithCurrentDate:[NSDate date]];
        
        /////  将需要转换的时间转换成 NSDate 对象
        NSDate * needFormatDate = [self getLocalDateWithCurrentDate:[NSDate dateWithTimeIntervalSince1970:ts]];
        /////  取当前时间和转换时间两个日期对象的时间间隔
        /////  这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:  typedef double NSTimeInterval;
        NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
        
        //// 再然后，把间隔的秒数折算成天数和小时数：
        
        NSString *dateStr = @"";
        
        if (time<=60) {  //// 1分钟以内的
            dateStr = @"刚刚";
        }else if(time<=60*60){  ////  一个小时以内的
            
            int mins = time/60;
            dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
            
        }else if(time <= 60*60*24){   //// 在两天内的
            
            [dateFormatter setDateFormat:@"YYYY/MM/dd"];
            NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
            NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
            
            [dateFormatter setDateFormat:@"HH:mm"];
            if ([need_yMd isEqualToString:now_yMd]) {
                //// 在同一天
                dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }else{
                ////  昨天
                dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }
        }else {
            
            [dateFormatter setDateFormat:@"yyyy"];
            NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [dateFormatter stringFromDate:nowDate];
            
            if ([yearStr isEqualToString:nowYear]) {
                ////  在同一年
                [dateFormatter setDateFormat:@"MM月dd日 HH:mm"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }else{
                [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }
        }
        
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
    
    
}

+ (NSString *)monthlyDetailTimeWithTimeStrap:(NSTimeInterval)ts {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"MM月dd日 HH:mm:ss";
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:ts];
    
    return [dateFormatter stringFromDate:date];
}

+ (BOOL)moreThanOneHourToNow: (NSDate *) theDate
{
    NSTimeInterval late = [theDate timeIntervalSince1970]*1;
    
    NSDate* dat = [NSDate date];
    NSTimeInterval now =[dat timeIntervalSince1970]*1;
    NSTimeInterval diff = now - late;
    if (diff/3600 > 1) {
        return YES;
    }
    return NO;
}

+ (NSDate *)getLocalDateWithCurrentDate:(NSDate *)date {
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Hong_Kong"];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localDate = [date dateByAddingTimeInterval:interval];
    return localDate;
}

+ (NSString *)imTimerShaftStringWithTimeStamp:(NSTimeInterval)ts {
    @try {
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
        dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
        NSDate * nowDate = [self getLocalDateWithCurrentDate:[NSDate date]];
        /////  将需要转换的时间转换成 NSDate 对象
        NSDate * needFormatDate = [self getLocalDateWithCurrentDate:[NSDate dateWithTimeIntervalSince1970:ts]];
        
        NSString *dateStr = @"";
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *dayStr = [dateFormatter stringFromDate:needFormatDate];
        NSString *nowday = [dateFormatter stringFromDate:nowDate];
        NSString *yesterday = [dateFormatter stringFromDate:[nowDate dateByAddingTimeInterval:-60*60*24]];
        if ([dayStr isEqualToString:nowday]) {  //// 今天
            [dateFormatter setDateFormat:@"HH:mm"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else if([dayStr isEqualToString:yesterday]){  ////昨天
            [dateFormatter setDateFormat:@"昨天 HH:mm"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else {
            [dateFormatter setDateFormat:@"yyyy"];
            NSString *yearStr = [dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [dateFormatter stringFromDate:nowDate];
            
            if ([yearStr isEqualToString:nowYear]) {
                ////  在同一年
                [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }else{
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }
        }
        
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
}

+ (NSString *)imListTimerShaftStringWithTimeStamp:(NSTimeInterval)ts {
    @try {
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
        NSDate * nowDate = [self getLocalDateWithCurrentDate:[NSDate date]];
        /////  将需要转换的时间转换成 NSDate 对象
        NSDate * needFormatDate = [self getLocalDateWithCurrentDate:[NSDate dateWithTimeIntervalSince1970:ts]];
        
        NSString *dateStr = @"";
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *dayStr = [dateFormatter stringFromDate:needFormatDate];
        NSString *nowday = [dateFormatter stringFromDate:nowDate];
        NSString *yesterday = [dateFormatter stringFromDate:[nowDate dateByAddingTimeInterval:-60*60*24]];
        if ([dayStr isEqualToString:nowday]) {  //// 今天
            [dateFormatter setDateFormat:@"HH:mm"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else if([dayStr isEqualToString:yesterday]){  ////昨天
            [dateFormatter setDateFormat:@"昨天 HH:mm"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else {
            [dateFormatter setDateFormat:@"yyyy"];
            NSString *yearStr = [dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [dateFormatter stringFromDate:nowDate];
            
            if ([yearStr isEqualToString:nowYear]) {
                ////  在同一年
                //是否是同一个月
                [dateFormatter setDateFormat:@"MM"];
                NSString *monthStr = [dateFormatter stringFromDate:needFormatDate];
                NSString *nowmonth = [dateFormatter stringFromDate:nowDate];
                if ([monthStr isEqualToString:nowmonth]) {
                    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                    dateStr = [dateFormatter stringFromDate:needFormatDate];
                }else {
                    [dateFormatter setDateFormat:@"MM-dd"];
                    dateStr = [dateFormatter stringFromDate:needFormatDate];
                }
            }else{
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }
        }
        
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
}

+ (NSString *)getLiveDateWithTimeStrap:(NSTimeInterval)ts {
    @try {
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
        NSDate * nowDate = [self getLocalDateWithCurrentDate:[NSDate date]];
        /////  将需要转换的时间转换成 NSDate 对象
        NSDate * needFormatDate = [self getLocalDateWithCurrentDate:[NSDate dateWithTimeIntervalSince1970:ts]];
        
        NSString *dateStr = @"";
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *dayStr = [dateFormatter stringFromDate:needFormatDate];
        NSString *nowday = [dateFormatter stringFromDate:nowDate];
        NSString *yesterday = [dateFormatter stringFromDate:[nowDate dateByAddingTimeInterval:-60*60*24]];
        if ([dayStr isEqualToString:nowday]) {  //// 今天
            [dateFormatter setDateFormat:@"HH:mm:ss"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else if([dayStr isEqualToString:yesterday]){  ////昨天
            [dateFormatter setDateFormat:@"昨天 HH:mm:ss"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else {
            [dateFormatter setDateFormat:@"yyyy"];
            NSString *yearStr = [dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [dateFormatter stringFromDate:nowDate];
            
            if ([yearStr isEqualToString:nowYear]) {
                ////  在同一年
                [dateFormatter setDateFormat:@"MM-dd HH:mm:ss"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }else{
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }
        }
        
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
}
+ (NSString *)dayTimeStringWithTS:(NSTimeInterval)ts {
    return [self dateWithFormat:@"HH:mm" WithTS:ts];
}

+ (NSString *)dateTimeStringWithTS:(NSTimeInterval)ts {
    return [self dateWithFormat:@"yyyy-MM-dd" WithTS:ts];
}

+ (NSString *)monthTimeStringWithTs:(NSTimeInterval)ts {
    return [self dateWithFormat:@"yyyy年MM月" WithTS:ts];
}

+ (NSString *)detailTimeStringWithTs:(NSTimeInterval)ts {
    return [self dateWithFormat:@"yyyy-MM-dd HH:mm:ss" WithTS:ts];
}


+ (NSString*)dateWithFormat:(NSString*)format WithTS:(NSTimeInterval)ts {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = format;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:ts];
    
    return [dateFormatter stringFromDate:date];
}


+(NSDate*)utcdate:(NSString*)str withFormat:(NSString*)format{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:str];
    return date;
}

+ (NSInteger)compareDate:(NSDate*)aDate withDate:(NSDate*)bDate
{
    NSInteger aa = -1;
    NSDate *dta = aDate;
    NSDate *dtb = bDate;
    NSComparisonResult result = [dta compare:dtb];
    if (result==NSOrderedSame)
    {
        //相等
        aa = 0;
    }else if (result==NSOrderedAscending) {
        //bDate比aDate大
        aa = 1;
    }else if (result==NSOrderedDescending) {
        //bDate比aDate小
        aa = -1;
    }
    return aa;
}


@end




